﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using System.Data.SqlClient;

namespace POSPlus.DatabaseOperations
{
	public class ProdBat:ProductBatches
	{

		public ProdBat(String upc, String batchNo, int locAisle, int locBlock, int locSeq, String svtRegPrintTagFlag)
		{
			Upc = upc;
			BatchNo = batchNo;
			LocAisle = locAisle;
			LocBlock = locBlock;
			LocSeq = locSeq;
			SVTregPrintTagFlag = svtRegPrintTagFlag;
		}

		public ProdBat(string upc, string batchNo, int locAisle, int locBlock, int locSeq, string svtRegPrintTagFlag, string shelfTagType)
		{
			Upc = upc;
			BatchNo = batchNo;
			LocAisle = locAisle;
			LocBlock = locBlock;
			LocSeq = locSeq;
			SVTregPrintTagFlag = svtRegPrintTagFlag;
			ShelfTagType = shelfTagType;
		}

		public ProdBat(String connectionString, String upc, String[] batchnos, int locAisle, 
			int locBlock, int locSeq)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				String batches = "(";
				for (int i = 0; i < batchnos.Length; i++)
				{
					batches += "BatchNo = @BatchNo" + i + " OR ";
				}
				batches = batches.Substring(0, batches.Length - 3);
				batches += ")";
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_TagLoc_ProdBats WHERE Upc = @Upc AND " +
					batches + " AND LocAisle = @LocAisle AND LocBlock = @LocBlock AND LocSeq = @LocSeq", connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					command.Parameters.AddWithValue("@LocAisle", locAisle);
					command.Parameters.AddWithValue("@LocBlock", locBlock);
					command.Parameters.AddWithValue("@LocSeq", locSeq);

					for (int i = 0; i < batchnos.Length; i++)
					{
						string batchno = batchnos[i];
						command.Parameters.AddWithValue("@BatchNo" + i, batchno);
					}
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							Upc = FromDB<String>(reader["Upc"]);
							BatchNo = FromDB<String>(reader["BatchNo"]);
							LocAisle = FromDB<int>(reader["LocAisle"]);
							LocBlock = FromDB<int>(reader["LocBlock"]);
							LocSeq = FromDB<int>(reader["LocSeq"]);
							SVTregPrintTagFlag = FromDB<String>(reader["SVTregPrintTagFlag"]);
							ShelfTagType = FromDB<String>(reader["ShelfTagType"]);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
		}

		public ProdBat(String connectionString, String upc, String[] batchnos)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				String batches = "(";
				for (int i = 0; i < batchnos.Length; i++)
				{
					batches += "BatchNo = @BatchNo" + i + " OR ";
				}
				batches = batches.Substring(0, batches.Length - 3);
				batches += ")";
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_TagLoc_ProdBats WHERE Upc = @Upc AND " +
					batches, connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					for (int i = 0; i < batchnos.Length; i++)
					{
						string batchno = batchnos[i];
						command.Parameters.AddWithValue("@BatchNo" + i, batchno);
					}
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							Upc = FromDB<String>(reader["Upc"]);
							BatchNo = FromDB <String>(reader["BatchNo"]);
							LocAisle = FromDB<int>(reader["LocAisle"]);
							LocBlock = FromDB<int>(reader["LocBlock"]);
							LocSeq = FromDB<int>(reader["LocSeq"]);
							SVTregPrintTagFlag = FromDB<String>(reader["SVTregPrintTagFlag"]);
							ShelfTagType = FromDB<string>(reader["ShelfTagType"]);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
		}

		public ProdBat(String connectionString, String upc, String batchNo)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_TagLoc_ProdBats WHERE Upc = @Upc" +
					"BatchNo = @BatchNo", connection))
				{
					command.Parameters.AddWithValue("@Upc", upc);
					command.Parameters.AddWithValue("@BatchNo", batchNo);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							Upc = FromDB<String>(reader["Upc"]);
							BatchNo = FromDB<String>(reader["BatchNo"]);
							LocAisle = FromDB<int>(reader["LocAisle"]);
							LocBlock = FromDB<int>(reader["LocBlock"]);
							LocSeq = FromDB<int>(reader["LocSeq"]);
							SVTregPrintTagFlag = FromDB<String>(reader["SVTregPrintTagFlag"]);
							ShelfTagType = FromDB<string>(reader["ShelfTagType"]);
						}
					}
				}
				connection.Close();
			}
		}

		public static List<ProdBat> GetAllByBatchNo(String connectionString, String batchNo)
		{
			List<ProdBat> bats = new List<ProdBat>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT Upc, BatchNo, LocAisle, LocBlock, LocSeq,SVTregPrintTagFlag,ShelfTagType " +
					"FROM POSplus_TagLoc_ProdBats WHERE BatchNo = @BatchNo", connection))
				{
					command.Parameters.AddWithValue("@BatchNo", batchNo);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							bats.Add(new ProdBat(
								FromDB<String>(reader["Upc"]),
								FromDB<String>(reader["BatchNo"]),
								FromDB<int>(reader["LocAisle"]),
								FromDB<int>(reader["LocBlock"]),
								FromDB<int>(reader["LocSeq"]),
								FromDB<String>(reader["SVTregPrintTagFlag"]),
								FromDB<string>(reader["ShelfTagType"])));
						}
					}
				}
				connection.Close();
			}
			return bats;
		}

		public static List<ProdBat> GetAllByBatchNo(String connectionString, String batchNo, char printTagFlag, DateTime startDate, DateTime endDate)
		{
			List<ProdBat> bats = new List<ProdBat>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT Upc, BatchNo, LocAisle, LocBlock, LocSeq, SVTregPrintTagFlag, Modified, ShelfTagType " +
					"FROM POSplus_TagLoc_ProdBats WHERE BatchNo = @BatchNo AND SVTregPrintTagFlag = @SVTregPrintTagFlag AND "
					+"Modified BETWEEN @StartDate AND @EndDate", connection))
				{
					command.Parameters.AddWithValue("@BatchNo", batchNo);
					command.Parameters.AddWithValue("@SVTregPrintTagFlag", printTagFlag);
					command.Parameters.AddWithValue("@StartDate", startDate);
					command.Parameters.AddWithValue("@EndDate", endDate);

					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							bats.Add(new ProdBat(
								FromDB<String>(reader["Upc"]),
								FromDB<String>(reader["BatchNo"]),
								FromDB<int>(reader["LocAisle"]),
								FromDB<int>(reader["LocBlock"]),
								FromDB<int>(reader["LocSeq"]),
								FromDB<String>(reader["SVTregPrintTagFlag"]),
								FromDB<string>(reader["ShelfTagType"])));
						}
					}
				}
				connection.Close();
			}
			return bats;
		}

		public static List<ProdBat> GetAllByBatchNo(String connectionString, String batchNo, char printTagFlag)
		{
			List<ProdBat> bats = new List<ProdBat>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT Upc, BatchNo, LocAisle, LocBlock, LocSeq, SVTregPrintTagFlag, ShelfTagType " +
					"FROM POSplus_TagLoc_ProdBats WHERE BatchNo = @BatchNo AND SVTregPrintTagFlag = @SVTregPrintTagFlag", connection))
				{
					command.Parameters.AddWithValue("@BatchNo", batchNo);
					command.Parameters.AddWithValue("@SVTregPrintTagFlag", printTagFlag);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							bats.Add(new ProdBat(
								FromDB<String>(reader["Upc"]),
								FromDB<String>(reader["BatchNo"]),
								FromDB<int>(reader["LocAisle"]),
								FromDB<int>(reader["LocBlock"]),
								FromDB<int>(reader["LocSeq"]),
								FromDB<String>(reader["SVTregPrintTagFlag"]),
								FromDB<string>(reader["ShelfTagType"])));
						}
					}
				}
				connection.Close();
			}
			return bats;
		}

		protected override void PopulateDefault()
		{
			throw new NotImplementedException();
		}

		public override void Create()
		{
			throw new NotImplementedException();
		}

		public override void Update()
		{
			throw new NotImplementedException();
		}

		public override void Delete()
		{
			throw new NotImplementedException();
		}
	}
}
